package com.dycx.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.system.dto.RolePermissionDTO;
import com.dycx.system.entity.RolePermissionDO;
import com.dycx.system.mapper.RoleActionMapper;
import com.dycx.system.service.RolePermissionService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class RolePermissionImpl extends ServiceImpl<RoleActionMapper, RolePermissionDO> implements RolePermissionService {

    /**
     * 添加
     *
     * @param
     * @return
     */
    @Override
    public Boolean insert(RolePermissionDTO dto) {
        List<String> rolePermissionIds = this.getPermissionIds(dto.getRoleId());

        //删除权限
        List<String> delIds = new ArrayList<>();
        rolePermissionIds.forEach(permissionId -> {
            if (!dto.getPermissionIds().contains(permissionId)) {
                delIds.add(permissionId);
            }
        });
        if (delIds.size() > 0) {
            LambdaQueryWrapper<RolePermissionDO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(RolePermissionDO::getRoleId, dto.getRoleId());
            queryWrapper.in(RolePermissionDO::getPermissionId, delIds);
            this.remove(queryWrapper);
        }

        //增加权限
        dto.getPermissionIds().forEach(permissionId -> {
            if (!rolePermissionIds.contains(permissionId)) {
                RolePermissionDO ra = new RolePermissionDO();
                ra.setRoleId(dto.getRoleId());
                ra.setPermissionId(permissionId);
                this.save(ra);
            }
        });

        return true;
    }

    @Override
    public List<String> getPermissionIds(int roleId) {
        LambdaQueryWrapper<RolePermissionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(RolePermissionDO::getId);
        queryWrapper.eq(RolePermissionDO::getRoleId, roleId);

        List<RolePermissionDO> entityList = this.list(queryWrapper);

        return entityList.stream().map(RolePermissionDO::getPermissionId).collect(Collectors.toList());
    }

    @Override
    public List<String> getPermissionIdsByRoleIds(List<Integer> roleIds) {
        LambdaQueryWrapper<RolePermissionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(RolePermissionDO::getRoleId, roleIds);

        List<RolePermissionDO> entityList = this.list(queryWrapper);

        return entityList.stream().map(RolePermissionDO::getPermissionId).collect(Collectors.toList());
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    @Override
    public Boolean delete(Integer roleId) {
        LambdaQueryWrapper<RolePermissionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermissionDO::getRoleId, roleId);

        return this.remove(queryWrapper);
    }
}
